<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-26
 * Time: 15:43
 */

namespace App\Http\Controllers\Cruise\Merchant;


use App\Enums\BaseErrorCode;
use App\Http\Controllers\BaseController;
use App\Remote\Rms\RmsUserRemote;
use App\Service\Cruise\Shop\CruiseShopLicenseRecordsLogService;
use App\Service\Cruise\Shop\CruiseShopLicenseRecordsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CruiseShopLicenseController extends BaseController
{
    public function info(Request $request)
    {
        $shopId = (int)$request->header('shopId');

        return $this->success(CruiseShopLicenseRecordsService::getInfo($shopId));
    }

    public function update(Request $request)
    {
        $shopId = (int)$request->header('shopId');
        $uid    = getRmsUserInfoByAuth()->id;

        $managerUids = array_column(
            RmsUserRemote::getShopManagerUserByShopId([$shopId])['lists'] ?? [], 'userId'
        );

        if (!in_array(getRmsUserId(), $managerUids)) {
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, '无店长权限');
        }

        $input = $request->all();

        $validator = Validator::make($input, [
            'businessLicense.id'                      => 'integer',
            'businessLicense.url'                     => 'required|string',
            'businessLicense.licenceNumber'           => 'string',
            'businessLicense.name'                    => 'required|string',
            'businessLicense.licenseEndDate'          => 'string',
            'businessLicense.licenseEndDateIsForever' => 'required|integer',
            'businessLicense.businessScope'           => 'string',
            'businessLicense.ocrResult'               => 'array',

            'foodLicense.id'                      => 'integer',
            'foodLicense.url'                     => 'required|string',
            'foodLicense.licenceNumber'           => 'string',
            'foodLicense.name'                    => 'required|string',
            'foodLicense.licenseEndDate'          => 'string',
            'foodLicense.licenseEndDateIsForever' => 'required|integer',
            'foodLicense.mainBusiness'            => 'required|string',
            'foodLicense.businessScope'           => 'required|string',
            'foodLicense.ocrResult'               => 'array',
        ], [
            'businessLicense.url.required'                     => "营业执照不能为空",
//            'businessLicense.licenceNumber.required'           => "营业执照编号不能为空",
            'businessLicense.name.required'                    => "营业执照名称不能为空",
            'businessLicense.licenseEndDateIsForever.required' => "营业执照是否过期不能为空",

            'foodLicense.url.required'                     => "食品经营许可证不能为空",
//            'foodLicense.licenceNumber.required'           => "食品经营许可证编号不能为空",
            'foodLicense.name.required'                    => "食品经营许可证名称不能为空",
            'foodLicense.mainBusiness.required'            => "食品经营许可证主体业态不能为空",
            'foodLicense.businessScope.required'            => "食品经营许可证主体业态不能为空",
            'foodLicense.licenseEndDateIsForever.required' => "食品经营许可证是否过期不能为空",
        ]);

        if ($validator->fails()) {
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, join(';', $validator->errors()->all()));
        }

        $error = [];
        if (!$input['businessLicense']['licenseEndDateIsForever'] && empty($input['businessLicense']['licenseEndDate'])){
            $error[] = '营业执照时间过期时间必填';
        }
        if (!$input['foodLicense']['licenseEndDateIsForever'] && empty($input['foodLicense']['licenseEndDate'])){
            $error[] = '食品经营许可证过期时间必填';
        }

        if ($error){
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, join(';', $error));
        }

        CruiseShopLicenseRecordsService::update(
            $uid, $shopId, $input['businessLicense'], $input['foodLicense']
        );

        return $this->success();
    }

}